package com.zhangtai.modules.dao;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.entity.system.RoleMenuEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 角色与菜单对应关系
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-08-10 14:44:39
 */
@Mapper
public interface RoleMenuDao extends BaseMapper<RoleMenuEntity> {


	@Select("SELECT u.*,m.* FROM sys_role_menu ur "
			+ "LEFT JOIN sys_role u ON ur.role_id = u.role_id "
			+ "LEFT JOIN sys_menu m ON ur.menu_id = m.menu_id")
	public List<JSONObject> getRoleMenuList(Page<JSONObject> page);

	@Delete("DELETE FROM sys_role_menu WHERE role_id = #{roleId}")
	public void deleteRoleMenu(@Param("roleId") Long roleId);

	@Delete("DELETE FROM sys_role_menu WHERE role_id=#{roleId} and menu_id in (select menu_id from sys_menu  where type=#{type}) ")
	public void deleteRoleMenuAndAuthority(@Param("roleId") Long roleId,@Param("type") Integer type);
	
	@Update("UPDATE sys_role_menu m set m.is_delete = 1 WHERE m.role_id = #{roleId}")
	public void updateRoleMenu(@Param("roleId") Long roleId);

	@Select("SELECT rm.menu_id,rm.role_id,rm.is_delete,r.role_name,m.name FROM sys_role_menu rm "
			+ "LEFT JOIN sys_role r ON rm.role_id = r.role_id "
			+ "LEFT JOIN sys_menu m ON m.menu_id = rm.menu_id  where rm.role_id = #{roleId} and m.type=#{type}   and rm.is_delete='0'")
	public List<JSONObject> getRoleMenuById(@Param("roleId") Long roleId,@Param("type") String type);
}
